package com.shop.item.api.controller;

import com.shop.common.entity.item.Category;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.DateUtil;
import com.shop.common.util.state.ResultCode;
import com.shop.item.api.service.IICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/item/category/")
@CrossOrigin
public class ItemCategoryController extends BaseController {
    @Autowired
    private IICategoryService categoryService;

    @GetMapping("categoryList")
    public String apiGetCategoryList() {
        List<Category> list = new ArrayList<>();
        ServiceBean<List<Category>> serviceBean = categoryService.getCategoryList();
        return returnJSON(ResultCode.SUCCESS.value(), "", serviceBean.getData());
    }

    @GetMapping("addCategory")
    public String apiAddCategory(String name) {
        InputBean<Category> inputBean = new InputBean<>();
        Category category = new Category();
        category.setCtgName(name);
        category.setDataStatus(0);
        category.setCreateTime(DateUtil.newNowDate());
        category.setCtgLevel(1);
        category.setCtgPId(0);
        inputBean.setParam(category);
        categoryService.addCategory(inputBean);
        return returnSuccess();
    }
}
